package com.source.module_kotlin_library.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v7.app.AppCompatActivity
import com.source.module_kotlin_library.R
import com.source.module_kotlin_library.fragment.AttentionFragment
import com.source.module_kotlin_library.fragment.USFragment
import com.source.module_kotlin_library.fragment.WorldFragment
import kotlinx.android.synthetic.main.activity_kt_main.*

class KTMainActivity : AppCompatActivity() {
    //fragment集合
    var fragmentList = ArrayList<Fragment>()

    //title集合
    var titleList = ArrayList<String>()

    companion object {
        fun startActivity(context: Context) {
            var intent = Intent()
            intent.setClass(context, KTMainActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kt_main)
        initView()
        initData()
        initListener()
    }

    fun initView() {}

    fun initData() {
        fragmentList.add(WorldFragment())
        fragmentList.add(AttentionFragment())
        fragmentList.add(USFragment())
        titleList.add("世界")
        titleList.add("关注")
        titleList.add("热门")
    }

    fun initListener() {
        viewPager.adapter = MAdapter(this.supportFragmentManager)
        tabLayout.setupWithViewPager(viewPager)
    }

    private inner class MAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        override fun getCount(): Int {
            return fragmentList.size
        }

        override fun getItem(position: Int): Fragment {
            return fragmentList[position]
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titleList[position]
        }
    }

}